package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void saveOrUpdate(Department department) {
        if (department.getId() == null) {
            departmentMapper.insert(department);
        } else {
            departmentMapper.updateByPrimaryKey(department);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        if(id != null){
            departmentMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Department selectOne(Long id) {
        Department department = departmentMapper.selectByPrimaryKey(id);
        return department;
    }

    @Override
    public List<Department> selectAll() {
        List<Department> departments = departmentMapper.selectAll();
        return departments;
    }

    @Override
    public PageInfo<Department> query(QueryObject qo) {
        //开始分页,会拦截下一个查询的sql
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Department> departments = departmentMapper.queryPage(qo); //会被拦截并加上limit
        return new PageInfo<>(departments);
    }
}
